//
//  PYBottomLineField.m
//  catuncel
//
//  Created by Jim on 2017/11/17.
//  Copyright © 2017年 Jim. All rights reserved.
//

#import "PYBottomLineFieldView.h"

#import "UIImage+PYImageManager.h"

@interface PYBottomLineFieldView () <UITextFieldDelegate>
{
    BOOL _isShrink;
}

@property (nonatomic, strong) UIButton *eyes;

@property (nonatomic, strong) UIImageView *imgView;

@end

@implementation PYBottomLineFieldView

- (UITextField *)textField
{
    if (!_textField) {
        
        _textField = [[UITextField alloc] init];
        
        _textField.font = [UIFont boldSystemFontOfSize:15];

        _textField.backgroundColor = [UIColor clearColor];
        
        _textField.delegate = self;
    }
    
    return _textField;
}

- (UILabel *)lbl
{
    if (!_lbl) {
        
        _lbl = [UILabel new];
        
        _lbl.font = [UIFont systemFontOfSize:15];
        
    }
    
    return _lbl;
}

- (void)setBottomLineColor:(UIColor *)bottomLineColor
{
    _bottomLineColor = bottomLineColor;
    
    [self setNeedsDisplay];
}

- (void)setString:(NSString *)string
{
    _string = string;
    
    if (string) {
        
        CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.lbl.font} context:nil];
        
        self.lbl.text = string;
        
        if (self.leftImage) {
            
            self.lbl.frame = CGRectMake(25, 10 + (50 - 10 - 15) / 2, rect.size.width, 15);

        } else {
            
            self.lbl.frame = CGRectMake(0, 10 + (50 - 10 - 15) / 2, rect.size.width, 15);

        }
    }
}

- (void)setStringColor:(UIColor *)stringColor
{
    _stringColor = stringColor;
    
    self.lbl.textColor = stringColor;
}

- (UIButton *)eyes
{
    if (!_eyes) {
        
        _eyes = [[UIButton alloc] init];
        
        UIImage *img;
        
        if (self.imageColor) {
            
            img = [[UIImage imageNamed:@"mima_yincang_icon"] imageWithColor:self.imageColor];
            
        } else {
            
            img = [UIImage imageNamed:@"mima_yincang_icon"];
            
        }
        
        [_eyes setImage:img forState:UIControlStateNormal];
        
        [_eyes setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
        
        [_eyes addTarget:self action:@selector(showPassword) forControlEvents:UIControlEventTouchUpInside];
        
        _eyes.hidden = YES;
    }
    
    return _eyes;
}

- (void)setImageColor:(UIColor *)imageColor
{
    _imageColor = imageColor;
    
    UIImage *img = [self.eyes.currentImage imageWithColor:_imageColor];
    
    [self.eyes setImage:img forState:UIControlStateNormal];
}

- (void)setShowEyes:(BOOL)showEyes
{
    _showEyes = showEyes;
    
    if (showEyes) {
        
        self.eyes.hidden = NO;
        
        self.textField.secureTextEntry = YES;
        
    } else {
        
        self.eyes.hidden = YES;
        
        self.textField.secureTextEntry = NO;

    }
}

- (UIImageView *)imgView
{
    if (!_imgView) {
        
        _imgView = [UIImageView new];
    }
    
    return _imgView;
}

- (void)setLeftImage:(UIImage *)leftImage
{
    _leftImage = leftImage;
    
    if (leftImage) {
        
        self.imgView.image = self.leftImage;
        
        CGRect rect = [self.lbl.text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.lbl.font} context:nil];
        
        self.lbl.frame = CGRectMake(25, 10 + (50 - 10 - 15) / 2, rect.size.width, 15);
        
        [self layoutSubviews];
    }
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview:self.imgView];
        
        [self addSubview:self.textField];
        
        [self addSubview:self.lbl];
        
        [self addSubview:self.eyes];

        self.backgroundColor = [UIColor clearColor];
        
        // 输入框编辑中的事件
        [self.textField addTarget:self action:@selector(textFieldValueChange) forControlEvents:UIControlEventAllEditingEvents];
    }
    return self;
}

- (void)layoutSubviews
{
    if (self.leftImage != nil) {
        
        self.textField.frame = CGRectMake(25, 10, self.bounds.size.width - 70, self.bounds.size.height - 10);
        
        self.imgView.frame = CGRectMake(0, 10 + (self.bounds.size.height - 10 - 15) / 2, 15, 15);
        
    } else {
        
        self.textField.frame = CGRectMake(0, 10, self.bounds.size.width - 45, self.bounds.size.height - 10);

    }
    
    self.eyes.frame = CGRectMake(self.bounds.size.width - 40, 10, 40, 40);
}

- (void)showPassword
{
    self.eyes.selected = !self.eyes.selected;
    
    if (self.eyes.selected) {
        
        UIImage *img;
        
        if (self.imageColor) {
            
            img = [[UIImage imageNamed:@"mima_xianshi_icon"] imageWithColor:self.imageColor];

        } else {
            
            img = [UIImage imageNamed:@"mima_xianshi_icon"];

        }
        
        [self.eyes setImage:img forState:UIControlStateNormal];
        
        self.textField.secureTextEntry = NO;
        
    } else {
        
        UIImage *img;
        
        if (self.imageColor) {
            
            img = [[UIImage imageNamed:@"mima_yincang_icon"] imageWithColor:self.imageColor];
            
        } else {
            
            img = [UIImage imageNamed:@"mima_yincang_icon"];
            
        }
        
        [self.eyes setImage:img forState:UIControlStateNormal];

        self.textField.secureTextEntry = YES;

    }
}

- (void)textFieldValueChange
{
    if (self.textField.text.length == 0) {
        
        if (_isShrink) {
            
            _isShrink = NO;

            [self expandAnimation];
        }
        
        _highlightBottomLine = NO;
        
        [self setNeedsDisplay];
    
    } else {
        
        if (!_isShrink) {
            
            _isShrink = YES;

            [self shrinkAnimation];
        }
       
        _highlightBottomLine = YES;
        
        [self setNeedsDisplay];
    }
}

- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();

    if (_highlightBottomLine) {
        
        if (_highlightBottomLineColor) {
            
            CGContextSetFillColorWithColor(context, _highlightBottomLineColor.CGColor);

        } else {
            
            CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:222.f/255.f alpha:1].CGColor);
            
        }
        
    } else {
        
        if (_bottomLineColor) {
            
            CGContextSetFillColorWithColor(context, _bottomLineColor.CGColor);

        } else {
            
            CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:222.f/255.f alpha:1].CGColor);

        }
        
    }
    
    
    CGContextFillRect(context, CGRectMake(0, CGRectGetHeight(self.frame) - 0.5, CGRectGetWidth(self.frame), 0.5));
}

- (void)expandAnimation
{
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        
        self.lbl.transform = CGAffineTransformIdentity;
    }];
}

- (void)shrinkAnimation
{
    
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        
        CGAffineTransform scaleTransform = CGAffineTransformMakeScale(2.f/3.f, 2.f/3.f);
        
        CGRect rect = [self.string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.lbl.font} context:nil];
        
        CGAffineTransform translationTransform = CGAffineTransformMakeTranslation(-rect.size.width * 1.f/6.f, -10 - (self.bounds.size.height - 10 - 15) / 2 - 15 * 1.f/6.f);
        
        CGAffineTransform contactTransform = CGAffineTransformConcat(scaleTransform, translationTransform);
        
        self.lbl.transform = contactTransform;
    }];
    
}

@end
